/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlconnect;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

/**
 *
 * @author salm
 */
public class SQLConnectionFactory
{
    private SQLConnectionFactory() {
    }
    
    public static SQLConnectionFactory getInstance() {
        return inst;
    }
    private static SQLConnectionFactory inst = new SQLConnectionFactory();
    
    public SQLConnection createConnection(String serverName, String dbName, String userName, String password) throws SQLException
    {
        return new SQLBaseConnection(serverName, dbName, userName, password);
    }
    
    private static final String ServerKey = "Server";
    private static final String DBKey = "Database";
    private static final String UserNameKey = "Account";
    private static final String PasswordKey = "Password";
    public SQLConnection createConnectionByConfigFile(String fileName) throws SQLException
    {
        try
        {
            Properties p = new Properties();
            p.load(new FileInputStream(fileName));
            String serverName = p.getProperty(ServerKey);
            String dbName = p.getProperty(DBKey);
            String userName = p.getProperty(UserNameKey);
            String password = p.getProperty(PasswordKey);
            
            return createConnection(serverName, dbName, userName, password);
        }
        catch (IOException ioEx)
        {
            return null;
        }
    }
}
